import time
from Common.Base import Base, open_browser

login_url = 'http://172.16.11.209:8080/ecshop/user.php'


class ReceivingAddressPage(Base):
    """封装表现层"""
    username_loc = ('name', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    submit_loc = ('name', 'submit')  # 登录按钮
    receiving_address_loc = ('xpath', '/html/body/div[6]/div[1]/div/div/div/div/a[4]')

    def get_login(self, username, password):
        """
        登录
        @param username: 用户名
        @param password: 密码
        @return:
        """
        self.send_keys(self.username_loc, username)
        self.send_keys(self.password_loc, password)
        self.click(self.submit_loc)

    def click_receiving_address(self):
        """点击收货地址"""
        self.click(self.receiving_address_loc)

    def address_locator(self, index='last()'):
        """
        将整个收货地址当做一个整体,定位
        :param index:
        :return:
        """
        # 省市区
        provinces_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[2]/option[24]')
        cities_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[3]/option[2]')
        districts_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[4]/option[5]')
        # 收货人姓名
        consignee_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[2]/input')
        # 邮箱
        email_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[4]/input')
        # 详细地址
        address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[2]/input')
        # 邮编
        zipcode_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[4]/input')
        # 电话
        telephone_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[2]/input')
        # 手机
        mobile_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[4]/input')
        # 确认修改
        update_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')
        # 删除
        delete_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input[2]')
        # 新增收货地址
        insert_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')
        if index == 'last()':
            return {'provinces_loc': provinces_loc,  # 省
                    'cities_loc': cities_loc,  # 市
                    'districts_loc': districts_loc,  # 区
                    'consignee_loc': consignee_loc,  # 收货人姓名
                    'email_loc': email_loc,  # 收货人邮箱
                    'address_loc': address_loc,  # 详细地址
                    'zipcode_loc': zipcode_loc,  # 邮编
                    'telephone_loc': telephone_loc,  # 电话号码
                    'mobile_loc': mobile_loc,  # 手机号码
                    'insert_address_loc': insert_address_loc  # 新增收货地址按钮
                    }
        else:
            return {'provinces_loc': provinces_loc,  # 省
                    'cities_loc': cities_loc,  # 市
                    'districts_loc': districts_loc,  # 区
                    'consignee_loc': consignee_loc,  # 收货人姓名
                    'email_loc': email_loc,  # 收货人邮箱
                    'address_loc': address_loc,  # 详细地址
                    'zipcode_loc': zipcode_loc,  # 邮编
                    'telephone_loc': telephone_loc,  # 电话号码
                    'mobile_loc': mobile_loc,  # 手机号码
                    'update_address_loc': update_address_loc,  # 修改收货地址
                    'delete_address_loc': delete_address_loc,  # 删除收货地址
                    }

    def get_provinces(self, index='last()'):
        """选择省份"""
        self.click(self.address_locator(index)['provinces_loc'])

    def get_cities(self, index='last()'):
        """选择市"""
        self.click(self.address_locator(index)['cities_loc'])

    def get_districts(self, index='last()'):
        """选择区"""
        self.click(self.address_locator(index)['districts_loc'])

    def input_consignee(self, name, index='last()'):
        """输入收货人姓名"""
        self.send_keys(self.address_locator(index)['consignee_loc'], name)

    def input_email(self, email, index='last()'):
        """输入收货人邮箱"""
        self.send_keys(self.address_locator(index)['email_loc'], email)

    def input_address(self, address, index='last()'):
        """输入详细地址"""
        self.send_keys(self.address_locator(index)['address_loc'], address)

    def input_zipcode(self, zipcode, index='last()'):
        """输入邮编"""
        self.send_keys(self.address_locator(index)['zipcode_loc'], zipcode)

    def input_telephone(self, telephone, index='last()'):
        """输入电话号码"""
        self.send_keys(self.address_locator(index)['telephone_loc'], telephone)

    def input_mobile(self, mobile, index='last()'):
        """输入手机号码"""
        self.send_keys(self.address_locator(index)['mobile_loc'], mobile)

    def click_insert_address(self, index='last()'):
        """点击新增收货地址"""
        self.click(self.address_locator(index)['insert_address_loc'])

    def click_update_address(self, index='last()'):
        """点击修改收货地址"""
        self.click(self.address_locator(index)['update_address_loc'])

    def click_delete_address(self):
        """点击删除收货地址"""
        self.click(self.address_locator("1")['delete_address_loc'])

    def is_add_success(self, index):
        """判断添加是否成功"""
        if self.get_element_attribute(self.address_locator(index)['consignee_loc']) == True:
            return True
        else:
            return False

    def click_popup_accept(self):
        """点击删除弹窗确定按钮"""
        new_alert = self.driver.switch_to.alert
        new_alert.accept()

    def click_popup_dismiss(self):
        """点击弹窗取消按钮"""
        alert = self.driver.switch_to.alert
        alert.dismiss()


if __name__ == '__main__':
    driver = ReceivingAddressPage(open_browser())
    driver.open_url(login_url)
    driver.get_login('name1', '456789')
    driver.click_receiving_address()
    driver.get_provinces()
    driver.get_cities()
    driver.get_districts()
    driver.input_consignee('张三')
    driver.input_email('2034384295@qq.com')
    driver.input_address('天府新谷')
    driver.input_zipcode('000000')
    driver.input_telephone('02855544478')
    driver.input_mobile('12548665214')
    driver.click_insert_address()
    time.sleep(1)
    driver.click_delete_address()
    time.sleep(1)
    driver.click_popup_accept()
    time.sleep(1)
    driver.close_browser()
